{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<b>Steps:</b>\n",
    "1. Use the twitteR library to fetch the tweets based on the keywords and save them as sql lite file\n",
    "2. Export the sql lite file as csv\n",
    "3. Import the csv and save all the tweets as text file"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Collect Tweets\n",
    "library(twitteR)\n",
    "\n",
    "# Enter your credentials\n",
    "consumer_key <- \"\"\n",
    "consumer_secret <- \"\"\n",
    "access_token <- \"\"\n",
    "access_secret <- \"\"\n",
    "setup_twitter_oauth(consumer_key, consumer_secret, access_token, access_secret)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# append your key words to the list\n",
    "tweet_key <- c(\"blockchain\",\"ethereum\",\"r3 corda\",\"bitcoin\",\"hyperledger\",\"crypto\",\"initial coin offering\", \"ether\", \"dapp\", \"litecoin\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# Creates a sql_lite_file with the tag name\n",
    "for (i in 1:length(tweet_key)) {\n",
    "    db2 <- RSQLite::datasetsDb() # Initialize a database\n",
    "    register_db_backend(db2) #Functions to setup a database backend for twitteR\n",
    "    sql_lite_file=tweet_key[i] #path to db file\n",
    "    register_sqlite_backend(sql_lite_file)  #create the DBI connection and call register_db_backend\n",
    "    tweetresult <- unclass(search_twitter_and_store(searchString = tweet_key[i],lang = 'en', retryOnRateLimit = 120)[[1]])\n",
    "}\n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note: Open the sql lite file and export the file as csv"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<b>SAVE TWEETS as Text format</b>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "mydata = read.csv(\"\") # path to the csv file\n",
    "tweets = mydata$text\n",
    "write.table(tweets, \"<filename>.txt\",quote = FALSE, row.names = FALSE, col.names = FALSE)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "R",
   "language": "R",
   "name": "ir"
  },
  "language_info": {
   "codemirror_mode": "r",
   "file_extension": ".r",
   "mimetype": "text/x-r-source",
   "name": "R",
   "pygments_lexer": "r",
   "version": "3.4.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
